package com.xebest.web.controller.venmanager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.xebest.api.dto.ParameterDTO;
import com.xebest.api.dto.XeVenAptitudeResponseDTO;
import com.xebest.api.service.XeVenAptitudeApiService;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.base.dto.BaseResponseDTO;
import com.xebest.config.ParamConfig;
import com.xebest.web.form.XeAptitudeCheckForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 资质上传图片相关
 *
 * @author 李姣姣
 */
@Controller
@RequestMapping("/xeaptitude")
public class XeVenAptitudeController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(XeVenAptitudeController.class);

    @Autowired
    private XeVenAptitudeApiService xeVenAptitudeApiService;
    @Autowired
    private ParamConfig paramConfig;
    @Autowired
    private Environment env;


    /**
     * 资质信息查询
     *
     * @author 李姣姣
     */
    @RequestMapping("/queryAptitudeInfo")
    @ResponseBody
    public BaseResponse queryAptitudeInfo() {
        log.info("资质管理--商户资质信息查询--start：");
        try {
            JSONObject userInfo = super.getErpLoginUserInfo();
            Preconditions.checkArgument(userInfo != null, "请先登录");
            ParameterDTO dto = new ParameterDTO();
            dto.setUserCode(userInfo.getString("userCode"));
            XeVenAptitudeResponseDTO aptitudeDto = xeVenAptitudeApiService.queryAptitudeInfo(dto);
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("aptitudeList", aptitudeDto.getAptitudeList());
            resMap.put("picServer", env.getProperty("erp.imgUrl"));
            return sendAjaxOK(resMap);
        } catch (Exception e) {
            log.info("资质管理--商户资质信息查询接口,异常:", e);
            return sendAjaxError();
        }
    }

    /**
     * 保存证照图片信息
     *
     * @author 李姣姣
     */
    @RequestMapping("/updateAptitudeInfo")
    @ResponseBody
    public BaseResponse updateAptitudeInfo(@RequestBody @Validated XeAptitudeCheckForm aptitudeForm,
                                           BindingResult result, HttpServletRequest req, String zoom) {
        log.info("资质管理--保存资质图片接口--start：");
        try {
            JSONObject userInfo = super.getErpLoginUserInfo();
            Preconditions.checkArgument(userInfo != null, "请先登录");
            boolean errors = result.hasErrors();
            String errorsMag = "参数有误";
            if (errors) {
                errorsMag = result.getFieldError().getDefaultMessage();
            }
            Preconditions.checkArgument(!errors, errorsMag);
            //补充校验修改的参数
            Preconditions.checkArgument(userInfo.getString("userCode") != null
                    && aptitudeForm.getApUrl() != null
                    && aptitudeForm.getApFlag() != null, "缺少必要参数");

            //封装对象
            XeVenAptitudeResponseDTO dto = JSON.parseObject(JSON.toJSONString(aptitudeForm), XeVenAptitudeResponseDTO.class);
            dto.setApStatus(1);
            dto.setUserCode(userInfo.getString("userCode"));
            dto.setModifyUser(userInfo.getString("loginName"));

            //apId不为空走修改
            if (aptitudeForm.getApId() != null) {
                BaseResponseDTO baseResponseDTO = xeVenAptitudeApiService.updateAptitudeInfo(dto);
                Preconditions.checkArgument("2000".equals(baseResponseDTO.getStatus()), baseResponseDTO.getMsg());
            } else {
                //apId为空走新增
                BaseResponseDTO baseResponseDTO = xeVenAptitudeApiService.insertAptitudeInfo(dto);
                Preconditions.checkArgument("2000".equals(baseResponseDTO.getStatus()), baseResponseDTO.getMsg());
            }
            return sendAjaxOK();
        } catch (IllegalArgumentException iae) {
            log.info("客户证照修改信息保存接口,异常:", iae);
            return sendAjaxError(iae.getMessage());
        } catch (Exception e) {
            log.info("客户证照修改信息保存接口,异常:", e);
            return sendAjaxError();
        }
    }


}
